from db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re



class FundCode:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        
    def fund_insert(self, code, name, url):
        utf8_code = code.encode("utf8")
        utf8_name = name.encode("utf8")
        sql = "insert into entity_jijin (Fcode, Fname, Furl, Fcreate_time, Fmodify_time) values "
        sql += "('" + code + "','" + name + "','" + url + "', now(), now());"
        print(sql)
        try:
            self.db.execute_not_safe(sql)
        except Exception as e:
            pass
        #print(str(res))
    def get_http_data(self):
        url = "http://fund.eastmoney.com/allfund.html"
        bs4_data = MyHttp.bs4_gb2312_data(url)
        datas = bs4_data.find_all(name="ul", attrs={'class':'num_right'})
        for d in datas:
            funds = d.find_all(name="a")
            for fund in funds:
                href = fund['href']
                fileds = re.split('[（）()]', fund.text)
                if 3 == len(fileds):
                    self.fund_insert(fileds[1], fileds[2], href)

if __name__ == '__main__':
    test = FundCode()
    test.get_http_data()